package com.copa.service.impl;

import com.copa.entity.IHttpMessage;
import com.copa.service.GroupService;
import com.copa.utils.MyGroupMessageUtil;
import com.copa.utils.MyPrivateMessageUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import static com.copa.constant.MessageType.*;

/**
 * @author copa
 * @createDate 2022-03-01 21:28
 * @function [@at,nickname=小c,wxid=wxid_2lebjvcq5von22]
 */
@Service
public class GroupServiceImpl implements GroupService {

    @Value("${group.realNum}")
    private String groupNum;

    @Override
    public void sendMessage(IHttpMessage message) {
        switch (message.getType()) {
            case TEXT_MESSAGE:
                sendTextMessage(message);
                break;
        }
    }

    @Override
    public void sendDailyNewsMessage(IHttpMessage iHttpMessage) {
        MyGroupMessageUtil.sendPicMessage(iHttpMessage.getTo_wxid(), groupNum, iHttpMessage.getMsg());
    }

    private void sendTextMessage(IHttpMessage message) {
        String msg = message.getMsg();
        if (msg.contains("[@at,nickname=小c,wxid=wxid_2lebjvcq5von22]")) {
            if (!msg.startsWith("[@at,nickname=小c,wxid=wxid_2lebjvcq5von22] ")) {
                MyGroupMessageUtil.sendGroupMsgAndAt(message.getRobot_wxid(),
                        message.getFrom_wxid(),
                        message.getFinal_from_wxid(),
                        // 由于没弄到微信名，只能随便代替
                        "某某某",
                        "指令错误！不鸟你啦~");
                return;
            }
            msg = msg.replace("[@at,nickname=小c,wxid=wxid_2lebjvcq5von22] ", "").trim();
            if (msg.startsWith("音乐 ")) {
                msg = msg.replace("音乐 ", "");
                String[] split = msg.split("#");
                if (split.length >= 2) {
                    MyPrivateMessageUtil.sendMusicMessage(message.getRobot_wxid(), message.getFrom_wxid(), split[0], "酷狗".equals(split[1]) ? 1 : 2);
                    return;
                }
                MyPrivateMessageUtil.sendMusicMessage(message.getRobot_wxid(), message.getFrom_wxid(), msg, 0);
                return;
            }
            MyGroupMessageUtil.sendGroupMsgAndAt(message.getRobot_wxid(),
                    message.getFrom_wxid(),
                    message.getFinal_from_wxid(),
                    // 由于没弄到微信名，只能随便代替
                    "某某某",
                    "指令错误！不鸟你啦~");
        }
    }
}
